<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class User {

    var $FirstName;
    var $LastName;

}

class Items {

    var $UserID;
    var $ItemName;
    var $ItemDesc;

}

class Sample extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->output->enable_profiler(TRUE);
    }

    public function index() {
        $this->truncate();
        $this->insert_array();
        $this->insert_object();
        $this->insert_batch_array();
        $this->update_object();
        $this->select_user_object();
        $this->select_user_array();
        $this->select_user_row();
        $this->select_user_row_array();
        $this->select_helper_test();
        $this->metainfo();
    }

    public function insert_array() {
        for ($i = 0; $i < 25; $i++) {
            $data = array(
                'FirstName' => uniqid('試験' . rand()),
                'LastName' => uniqid('試験' . rand()),
            );
            $this->db->insert('[user]', $data);
            $uid = $this->db->insert_id();
            $data = array(
                'UserID' => $uid,
                'ItemName' => uniqid('商品' . rand()),
                'ItemDesc' => uniqid('商品の解説' . rand()),
            );
            $this->db->insert('[items]', $data);
        }
    }

    public function insert_batch_array() {
        for ($i = 0; $i < 25; $i++) {
            $data = array(
                'FirstName' => '試験しめい' . $i,
                'LastName' => '試験なまえ' . $i,
            );
            $datas[] = $data;
        }
        $this->db->insert_batch('[user]', $datas);
    }

    public function select_user_object() {
        $query = $this->db->get('[user]');
        foreach ($query->result() as $row) {
            echo $row->FirstName;
            echo $row->LastName;
        }
    }

    public function select_user_array() {
        $query = $this->db->get('[user]');
        foreach ($query->result_array() as $row) {
            echo $row['FirstName'];
            echo $row['LastName'];
        }
    }

    public function select_user_row() {
        $query = $this->db->get('[user]');
        if ($query->num_rows(15) > 0) {
            $row = $query->row();
            echo $row->FirstName;
            echo $row->LastName;
        }
    }

    public function select_user_row_array() {
        $query = $this->db->get('[user]');
        if ($query->num_rows() > 0) {
            $row = $query->row_array(5);
            echo $row['FirstName'];
            echo $row['LastName'];
        }
    }

    public function select_helper_test() {
        $query = $this->db->get('[user]');
        echo $query->num_fields();
    }

    public function insert_object() {
        for ($i = 0; $i < 25; $i++) {
            $user = new User;
            $user->FirstName = uniqid('試験' . rand());
            $user->LastName = uniqid('試験' . rand());
            $this->db->insert('[user]', $user);
            $uid = $this->db->insert_id();

            $items = new Items();
            $items->UserID = $uid;
            $items->ItemName = uniqid('商品' . rand());
            $items->ItemDesc = uniqid('商品の解説' . rand());
            $this->db->insert('[items]', $items);
        }
    }

    public function update_object() {
        for ($i = 0; $i < 25; $i++) {
            $user = new User;
            $user->FirstName = uniqid('更新' . rand());
            $user->LastName = uniqid('更新' . rand());
            $this->db->where('ID', $i);
            $this->db->update('[user]', $user);
        }
    }

    public function metainfo() {
        var_dump($this->db->list_tables());
        echo $this->db->table_exists('[user]');
        var_dump($this->db->list_fields('[user]'));
        $query = $this->db->query('SELECT * FROM [user]');
        var_dump($query->list_fields());
        var_dump($this->db->field_exists('FirstName', 'user'));
        var_dump($this->db->field_data('user'));
        $query = $this->db->query('SELECT * FROM [user]');
        var_dump($query->field_data());
    }

    public function truncate() {
        $this->db->truncate('[user]');
        $this->db->truncate('[items]');
    }

}

/* End of file sample.php */
/* Location: ./application/controllers/sample.php */